package com.hyperf.spring.boot.autoconfigure.lock;

import com.hyperf.core.lock.executor.LockExecutor;
import com.hyperf.core.lock.executor.RedisTemplateLockExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author lizhaoyang
 */
@Configuration
@ConditionalOnClass(StringRedisTemplate.class)
@ConditionalOnProperty(prefix = "hyperf.lock", name = "enabled", havingValue = "true", matchIfMissing = true)
public class RedisLockExecutorAutoConfiguration {

    private StringRedisTemplate stringRedisTemplate;

    public RedisLockExecutorAutoConfiguration(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(LockExecutor.class)
    @ConditionalOnProperty(prefix = "hyperf.lock", name = "lock-type", havingValue = "redis")
    public LockExecutor lockExecutor() {
        return new RedisTemplateLockExecutor(stringRedisTemplate);
    }
}
